REM THIS SHOULD BE PROCESSED WITH TURBAN
DIM F$(10),FC$(9),P(8),G(8,10)
DIM H$(128):REM HOSTNAME
DIM D$(128):REM HOST DEVICESPEC
DIM M$(128):REM MESSAGE SENT/RCVD
DIM N$(32):REM OUR NAME
DIM ON$(32):REM OPPONENT'S NAME
DIM O$(128):REM OUTPUT STRING
POKE 65,0:REM NO SIO SOUNDS
T=1
GRAPHICS 7
F=ADR(F$)
FC=ADR(FC$)
FC$="\00\00\00\00\00\00\00\00\00"
PRINT "Welcome to Connect 4"
A=PEEK(106)-32
POKE 54279,A
PM=256*A
POKE 559,46
POKE 53277,3
POKE 623,4
POKE 53248,100
POKE 53256,0
POKE 53257,1
POKE 704,55
POKE 53248,155
FOR I=0 TO 4*128
DPOKE PM+I*2,0
NEXT I
F$="\00\10\7C\FE\FE\FE\FE\FE\7C\10"

? "ENTER PLAYER NAME"
INPUT #16,N$

? "ENTER OPPONENT NAME"
INPUT #16,ON$

50 REM DRAW BOARD & PLAY GAME
IF(M):REM IF NOT FIRST GAME, ERASE OLD BOARD AND RESET VARIABLES
	COLOR 0
	FOR X=1 TO 75
		PLOT 35,X:DRAWTO 115,X
	NEXT X
	
	M=0:REM TOTAL NUMBER OF TURNS (MOVES)
	FOR X=0 TO 8
		P(X)=0
	NEXT X	
	FOR X=0 TO 8
		FOR Y=0 TO 10
			G(X,Y)=0
		NEXT Y
	NEXT X
ENDIF

COLOR 3
FOR Y=10 TO 70 STEP 10
	FOR X=110 TO 40 STEP -10
		CIRCLE X,Y,5
	NEXT X
NEXT Y

REM PLAY GAME
WHILE 1
	T=NOT T
	COLOR T+1
	M=M+1
	IF (M=57)
		PRINT "TIE GAME"
		SOUND 0,200,10,8
		PAUSE 6
		SOUND 
		GOTO 50
	ENDIF 

	IF(T=ME)
		PRINT N$;"'s turn."
	ELSE
		PRINT ON$;"'s turn."
	ENDIF

	X=0
	WHILE X=0
		X=4
		IF T
			POKE 704,202
		ELSE 
			POKE 704,40
		ENDIF 
		MOVE F,PM+522,10

		IF (T=ME):REM PLAYER 1 TURN		
			WHILE STRIG(0)
				POKE 53248,75+10*X
				DELTA=(STICK(0)=7)-(STICK(0)=11)
				IF(DELTA)
					X=X+DELTA
					IF X<1
						X=8
					ENDIF 
					IF X>8
						X=1
					ENDIF
					REM SEND POSITION
					? #1;X
					PAUSE 4
				ENDIF
			WEND:REM LOCAL PLAYER PRESSED TRIGGER
			IF (P(X)=7):REM COLUMN ALREADY FULL
				SOUND 0,99,6,8
				PAUSE 5
				SOUND 
				X=0
			ENDIF
		ELSE:REM PLAYER 2 TURN
			WHILE STRIG(0)
				POKE 53248,75+10*X
				DELTA=(STICK(0)=7)-(STICK(0)=11)
				IF(DELTA)
					X=X+DELTA
					IF X<1
						X=8
					ENDIF 
					IF X>8
						X=1
					ENDIF
					REM SEND POSITION
					? #1;X
					PAUSE 4
				ENDIF
			WEND:REM LOCAL PLAYER PRESSED TRIGGER
			IF (P(X)=7):REM COLUMN ALREADY FULL
				SOUND 0,99,6,8
				PAUSE 5
				SOUND 
				X=0
			ENDIF
		ENDIF:REM IF (T=ME)
REM	ENDIF		
	WEND:REM WHILE X=0
		
	P(X)=P(X)+1
	G(X,P(X))=T+1
	FOR I=PM+512+10 TO PM+512+91-P(X)*10
		MOVE F,I,10
		POKE 53248,75+10*X
	NEXT I
	PAINT 30+(10*X),80-10*P(X)
	POKE 53248,0
	MOVE FC,PM+512+92-P(X)*10,9
	IF P(X)>3
		IF G(X,P(X))&G(X,P(X)-1)&G(X,P(X)-2)&G(X,P(X)-3)
			GOTO 200
		ENDIF 
	ENDIF 
	FOR X=1 TO 5
		FOR Y=1 TO 7
			IF G(X,Y)
				IF G(X,Y)&G(X+1,Y)&G(X+2,Y)&G(X+3,Y)
					GOTO 200
				ENDIF 
				IF G(X,Y)&G(X+1,Y+1)&G(X+2,Y+2)&G(X+3,Y+3)
					GOTO 200
				ENDIF 
				IF Y>3
					IF G(X,Y)&G(X+1,Y-1)&G(X+2,Y-2)&G(X+3,Y-3)
						GOTO 200
					ENDIF 
				ENDIF 
			ENDIF 
		NEXT Y
	NEXT X
WEND 

200 REM SOMEONE WON
IF(T=ME)
	PRINT N$;" WINS!!";
ELSE
	PRINT ON$;" WINS!!";
ENDIF

FOR X=1 TO 255
	POKE 708+T,X
	SOUND 0,RAND(255),10,10
NEXT X
SOUND 
GOTO 50
